package com.liuyuxi.day5_20_night.service.imp;

import java.io.File;
import java.math.BigDecimal;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuyuxi.day5_20_night.bean.Dishes;
import com.liuyuxi.day5_20_night.bean.Room;
import com.liuyuxi.day5_20_night.bean.Running;
import com.liuyuxi.day5_20_night.bean.Tem;
import com.liuyuxi.day5_20_night.dao.MyDao;
import com.liuyuxi.day5_20_night.service.MyService;

@Service
public class MyServiceImp implements MyService {

	@Autowired
	private MyDao dao;

	// 列表
	@Override
	public PageInfo<Running> list(Integer pageNum, Integer pageSize, Tem tem) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize);
		List<Running> list = dao.list(tem);
		PageInfo<Running> info = new PageInfo<Running>(list);
		return info;
	}

	// 包间列表
	@Override
	public List<Room> listRoom() {
		// TODO Auto-generated method stub
		return dao.listRoom();
	}

	// 删除
	@Override
	public boolean del(String id) {
		// TODO Auto-generated method stub
		try {
			Running running = dao.getRun(Integer.valueOf(id));
			String picture = running.getPicture();
			if (picture != null && !"".equals(picture)) {
				File file = new File("D:/", picture);
				System.err.println(file);
				boolean delete = file.delete();
				System.err.println(delete);
			}
			dao.del(id);
			dao.delDis(id);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}

	}

	// 菜单列表
	@Override
	public List<Dishes> listDis() {
		// TODO Auto-generated method stub
		return dao.listDis();
	}

	// 添加
	@Override
	public void add(@Valid Running running) {
		// TODO Auto-generated method stub

		// 查询菜价格
		BigDecimal account = new BigDecimal(0);// 定义总消费
		Integer[] ids = running.getIds();// 获取菜品id
		for (Integer id : ids) {
			if (id != null) {
				BigDecimal price = dao.selPrice(id);// 查询菜品单价
				account = account.add(price);// 相加
			}
		}
		running.setAccount(account);

		// 添加订单表
		dao.addrunning(running);

		// 修改包间状态
		String stu = "1";
		dao.updRoomStau(running.getRoomId(), stu);

		// 添加中间表
		Integer runId = running.getId();
		for (Integer disId : ids) {
			if (disId != null) {
				dao.addDis(runId, disId);
			}
		}

	}

	// 验证包间是否正在使用
	@Override
	public boolean rooming(Integer id) {
		// TODO Auto-generated method stub
		int rooming = dao.rooming(id);
		if (rooming == 1) {
			return true;
		}
		return false;
	}

	// 查找订单对象
	@Override
	public Running getRun(Integer id) {
		// TODO Auto-generated method stub
		return dao.getRun(id);
	}

	// 根据id查找菜品
	@Override
	public Integer[] getDis(Integer id) {
		// TODO Auto-generated method stub
		return dao.getDis(id);
	}

	// 修改
	@Override
	public void upd(@Valid Running running) {
		// TODO Auto-generated method stub
		dao.delDis(String.valueOf(running.getId()));
		// 查询菜价格
		BigDecimal account = new BigDecimal(0);// 定义总消费
		Integer[] ids = running.getIds();// 获取菜品id
		for (Integer id : ids) {
			if (id != null) {
				BigDecimal price = dao.selPrice(id);// 查询菜品单价
				account = account.add(price);// 相加
			}
		}
		running.setAccount(account);

		// 修改订单表
		dao.upd(running);

		// 添加中间表
		Integer runId = running.getId();
		for (Integer disId : ids) {
			if (disId != null) {
				dao.addDis(runId, disId);
			}
		}
	}

	// 结账
	@Override
	public void settle(Integer id, Integer roomId) {
		// TODO Auto-generated method stub
		dao.settle(id);

		// 修改包间状态
		String stu = "0";
		dao.updRoomStau(roomId, stu);
	}
}
